/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sismol.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sismol.data.GerenciadorCrud;
import sismol.data.GerenciadorFactory;

/**
 *
 * @author Koji
 */
public class TerminaisAction  extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        Long pontoId = Long.valueOf(request.getParameter("pontoId"));
        String horariosString = request.getParameter("horarios-saida");

        String[] horariosArray = horariosString.split(";");
        List<Long> horarios = new ArrayList<Long>();
        for (String horario : horariosArray) {
            Date date = new Date();
            Calendar c = new GregorianCalendar();
            c.setTime(date);
            String[] h = horario.split(":");
            Integer hora = Integer.valueOf(h[0]);
            Integer minutos = Integer.valueOf(h[1]);

            c.set(Calendar.HOUR_OF_DAY, hora);
            c.set(Calendar.MINUTE, minutos);

            horarios.add(c.getTimeInMillis());
        }

        GerenciadorCrud crud = GerenciadorFactory.getGerenciadorCrud();
        crud.criarHorariosTerminal(pontoId, horarios);

        RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/sucesso.jsp");
        rd.forward(request, response);
    }


    // <editor-fold defaultstate="collapsed" desc="MÃ©todos HttpServlet. Clique no sinal de + Ã  esquerda para editar o cÃ³digo.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
